export function colorToRGBA(color) {
    let list = color.match(/RGBA[(]([0-9]{1,3},){3}(1|0)(\.[0-9]+)?[)]/);
    if (list && list[0]) {
        return color;
    }

    let out = hexToRGBA(color);
    if (out) {
        return out;
    }

    return null;
}

export function hexToRGBA(hex) {
    let list = hex.match(/#([a-f]|[A-F]|[0-9]){6}/);
    let hex6 = list && list[0];
    if (!hex6) {
        list = hex.match(/#([a-f]|[A-F]|[0-9]){3}/);
        const hex3 = list && list[0];
        if (hex3) {
            hex6 = "#";
            for (let i = 1; i < hex3.length; i++) {
                hex6 += hex3[i] + hex3[i];
            }
        } else {
            return null;
        }
    }

    let out = "RGBA(";
    for (let i = 1; i < hex6.length; i += 2) {
        const n = parseInt(hex6.slice(i, i + 2), 16);
        if (!isNaN(n) && n >= 0 && n <= 255) {
            out += n + ",";
        } else {
            return null;
        }
    }
    return out + "1)";
}

export function getColorFormat(color) {
    let list = color.match(/#([a-f]|[A-F]|[0-9]){6}/);
    const hex6 = list && list[0];
    if (hex6) {
        return "hex";
    }

    list = color.match(/#([a-f]|[A-F]|[0-9]){3}/);
    const hex3 = list && list[0];
    if (hex3) {
        return "hex";
    }

    list = color.match(/RGBA[(]([0-9]{1,3},){3}(1|0)(\.[0-9]+)?[)]/);
    const rgba = list && list[0];
    if (rgba) {
        return "rgba";
    }

    return null;
}

/**弧度制转角度制 */
export function rad2deg(rad) {
    return 180 * rad / Math.PI;
}

/**弧度制转角度制 */
export function deg2rad(deg) {
    return deg * Math.PI / 180;
}